package com.example.myapplication.tack_30;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.request.Request;
import com.example.myapplication.R;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;

import org.json.JSONException;
import org.json.JSONObject;

public class PaymentPageActivity extends AppCompatActivity {
    private ImageView exit;
    private RadioGroup radioGroup;
    private Intent intent;
    private String orderNo;
    private TextView payment;
    private JSONObject jsonObject;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_32_activity_payment_page);
        init();
        initExit();
        cilckPayment();
    }

    private void cilckPayment() {
        payment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
                RadioButton radioButton = findViewById(checkedRadioButtonId);
                String s = radioButton.getText().toString();
                if(!TextUtils.isEmpty(s)){
                    try {
                        jsonObject.put("orderNo",orderNo);
                        jsonObject.put("paymentType",s);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String json = jsonObject.toString();
                    initPayment(json);
                }else{
                    Toast.makeText(PaymentPageActivity.this,"请选择支付方式",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void initPayment(String json) {
        String url = "prod-api/api/takeout/pay";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(PaymentPageActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        if(stateData.getCode()==200){
                            PaymentPageActivity.this.finish();
                        }
                    }
                });

            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }


    private void initExit() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PaymentPageActivity.this.finish();
            }
        });
    }

    private void init() {
        intent = getIntent();
        orderNo = intent.getStringExtra(Common.COMMON);
        jsonObject = new JSONObject();
        exit = findViewById(R.id.exit);
        payment = findViewById(R.id.payment);
        radioGroup = findViewById(R.id.radioGroup);
    }
}